/*
 * Word class is composed of 2 8-bit bytes therefore it is a 16-bit value
 * */

package cpu.datatypes;

public class Word {
	public Byte byteHigh = new Byte( );
	public Byte byteLow = new Byte( );

	// Default Constructor
	public Word( ) {
		byteHigh = new Byte( );
		byteLow = new Byte( );
	}
	// Copy Constructor
	public Word( Word word ) {
		byteHigh = new Byte( word.byteHigh );
		byteLow = new Byte( word.byteLow );
	}
	public void fromWord( Word w ) {
		for( int i = 0; i < 16; i++ )
			bit( i ).value = w.bit( i ).value; 
	}
	public void fromRegister( cpu.datastores.Register1 r ) {
		for( int i = 0; i < 1; i++ )
			bit( i ).value = r.bit( i ).value;
		for( int i = 1; i < 16; i++ )
			bit( i ).value = false;
	}
	public void fromRegister( cpu.datastores.Register2 r ) {
		for( int i = 0; i < 2; i++ )
			bit( i ).value = r.bit( i ).value;
		for( int i = 2; i < 16; i++ )
			bit( i ).value = false;
	}
	public void fromRegister( cpu.datastores.Register4 r ) {
		for( int i = 0; i < 4; i++ )
			bit( i ).value = r.bit( i ).value;
		for( int i = 4; i < 16; i++ )
			bit( i ).value = false;
	}
	public void fromRegister( cpu.datastores.Register5 r ) {
		for( int i = 0; i < 5; i++ )
			bit( i ).value = r.bit( i ).value;
		for( int i = 5; i < 16; i++ )
			bit( i ).value = false;
	}
	public void fromRegister( cpu.datastores.Register6 r ) {
		for( int i = 0; i < 6; i++ )
			bit( i ).value = r.bit( i ).value;
		for( int i = 6; i < 16; i++ )
			bit( i ).value = false;
	}
	public void fromRegister( cpu.datastores.Register16 r ) {
		fromWord( r );
	}
	public Byte element( int i ) {
		if( i == 0 ) 
			return byteLow;	// 0 : little endian
		return byteHigh;
	}
	public Bit bit( int i ) {
		assert( i >= 0 && i < 16 );
		
		if( i >= 0 && i <= 7 )
			return byteLow.bit( i );	// 0 : little endian
		return byteHigh.bit( i - 8 );
	}
	public int toJavaInt( ) {
		int accumulator = 0;
		for( int i = 0; i < 8; i++ ) {
			if( byteLow.bit( i ).value )
				accumulator += java.lang.Math.pow( 2, i );
		}
		for( int i = 0; i < 8; i++ ) {
			if( byteHigh.bit( i ).value )
				accumulator += java.lang.Math.pow( 2, i + 8 );
		}
		return accumulator;
	}
	public boolean fromJavaInt( int value ) {
		// note return value is overflow
		int accumulator = value;
		for( int i = 7; i >= 0; i-- ) {
			if( accumulator / java.lang.Math.pow( 2, i + 8 ) >= 1 ) {
				byteHigh.bit( i ).value = true;
				accumulator -= java.lang.Math.pow( 2, i + 8 );
			} else {
				byteHigh.bit( i ).value = false;
			}
		}
		for( int i = 7; i >= 0; i-- ) {
			if( accumulator / java.lang.Math.pow( 2, i ) >= 1 ) {
				byteLow.bit( i ).value = true;
				accumulator -= java.lang.Math.pow( 2, i );
			} else {
				byteLow.bit( i ).value = false;
			}
		}
		if( accumulator != 0 ) 
			return true;		// overflow
		return false;			// no overflow
	}
	public void systemoutprintln( ) {
		for( int i = 0; i < 16; i++ )
			if( bit( i ).value == true )
				System.out.print( '1' );
			else
				System.out.print( '0' );
		System.out.println( );
	}
}
